package com.yan.furns.web.servlet;

import com.yan.furns.entity.Member;
import com.yan.furns.service.MemberService;
import com.yan.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author Yzb
 * @version 1.0
 */
public class MemberServlet extends BasicServlet {
	private MemberService memberService = new MemberServiceImpl();

	protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		System.out.println("RegisterServlet doPost");
//		System.out.println("成功通过反射调用register方法");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String email = req.getParameter("email");

		String code = req.getParameter("code");
		String token =req.getParameter(KAPTCHA_SESSION_KEY);
		req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

		if (token != null && token.equalsIgnoreCase(code)) {
			if (!memberService.existUsername(username)) {
				Member member = new Member(null, username, password, email);
//			System.out.println("用户名可用~");
				if (memberService.registerMember(member)) {
//				System.out.println("注册成功~~");
					req.getRequestDispatcher("/views/member/register_ok.jsp")
							.forward(req, resp);
				} else {
//				System.out.println("注册error!");
					req.getRequestDispatcher("/views/member/register_fail.jsp")
							.forward(req, resp);
				}
			} else {
				req.setAttribute("active", "register");
//			System.out.println("用户" + username + "已存在, 不可以注册");
				req.getRequestDispatcher("/views/member/login.jsp")
						.forward(req, resp);
			}
		} else {
			req.setAttribute("active", "register");
			req.setAttribute("username", username);
			req.setAttribute("email", email);
			req.setAttribute("msg", "验证码不正确");
			req.getRequestDispatcher("/views/member/login.jsp")
					.forward(req, resp);
		}

	}

	protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		super.doPost(req, resp);
//		System.out.println("成功通过反射调用login方法");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
//		System.out.println("LoginServlet doPost: username=" + username + " password=" + password);

		Member member = memberService.login(username, password);
		if (member != null) {
			HttpSession session = req.getSession();
			session.setAttribute("member", member);
			req.getRequestDispatcher("/views/member/login_ok.jsp")
					.forward(req, resp);
		} else {
			// 展示错误信息
			req.setAttribute("msg", "用户名或者密码错误");
			// 回显用户名
			req.setAttribute("username", username);
			req.getRequestDispatcher("/views/member/login.jsp")
					.forward(req, resp);
		}
	}

//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
////		super.doPost(req, resp);
//		String action = req.getParameter("action");
//
//		if ("login".equals(action)) {
//			login(req, resp);
//		} else if ("register".equals(action)) {
//			register(req, resp);
//		} else {
//			System.out.println("请求参数错误");
//		}
//	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getSession().removeAttribute("member");
		req.getSession().invalidate();
		resp.sendRedirect(req.getContextPath());
	}
}
